home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / frontend_implementation / VideoDisplay.py < prev   
Encoding:
Python Source  |  2006-04-10  |  9.0 KB  |  261 lines

  1. import app
  2. import frontend
  3. import frontend_implementation
  4. import os
  5. import threading
  6. import template
  7. import util
  8. import re
  9.  
  10. _genMutator = frontend_implementation.HTMLDisplay._genMutator
  11. execChromeJS = frontend_implementation.HTMLDisplay.execChromeJS
  12.  
  13. ###############################################################################
  14. #### The Playback Controller                                               ####
  15. ###############################################################################
  16.  
  17. class PlaybackController (app.PlaybackControllerBase):
  18.     
  19.     def playItemExternally(self, itemID):
  20.         item = app.PlaybackControllerBase.playItemExternally(self, itemID)
  21.         # now play this item externally
  22.         moviePath = ""
  23.         try:
  24.             moviePath = os.path.normpath(item.getPath())
  25.             os.startfile(moviePath)
  26.         except:
  27.             print "DTV: movie %s could not be externally opened" % moviePath
  28.  
  29. ###############################################################################
  30. #### Right-hand pane video display                                         ####
  31. ###############################################################################
  32.  
  33. class VideoDisplay (app.VideoDisplayBase, frontend.HTMLDisplay):
  34.     "Video player shown in a MainFrame's right-hand pane."
  35.  
  36.     def __init__(self):
  37.         print "VideoDisplay init"
  38.         html = template.fillStaticTemplate("video-display-vlc", {'eventCookie':self.getEventCookie(),'dtvPlatform':'xul'})
  39.         frontend.HTMLDisplay.__init__(self,html)
  40.         app.VideoDisplayBase.__init__(self)
  41.         print "Display initialized"
  42.  
  43.     elapseRe = re.compile('elapsed=(-?\d+)')
  44.     lengthRe = re.compile('len=(-?\d+)')
  45.  
  46.     # The mutation functions.
  47.     videoPlay = _genMutator('videoPlay')
  48.     videoPause = _genMutator('videoPause')
  49.     videoReset = _genMutator('videoReset')
  50.     videoStop = _genMutator('videoStop')
  51.     videoFullscreen = _genMutator('videoFullscreen')
  52.     videoSetVolume = _genMutator('videoSetVolume')
  53.     videoSetRate = _genMutator('videoSetRate')
  54.     videoSetPos = _genMutator('videoSetPos')
  55.  
  56.     def initRenderers(self):
  57.         print "initRenderers"
  58.         self.renderers.append(VLCPluginRenderer())
  59.  
  60.     def selectItem(self, item):
  61.         print "VideoDisplay select item"
  62.         self.itemPath = item.getFilename()
  63.         app.VideoDisplayBase.selectItem(self, item)
  64.  
  65.     def play(self):
  66.         print "VideoDisplay play %s" % self.itemPath
  67.         app.VideoDisplayBase.play(self)
  68.         url = util.absolutePathToFileURL(self.itemPath)
  69.         self.videoPlay(url)
  70.  
  71.     def pause(self):
  72.         print "VideoDisplay pause"
  73.         app.VideoDisplayBase.pause(self)
  74.         self.videoPause()
  75.  
  76.     def stop(self):
  77.         print "VideoDisplay stop"
  78.         self.videoStop()
  79.         app.VideoDisplayBase.stop(self)
  80.     
  81.     def goFullScreen(self):
  82.         print "VideoDisplay fullscreen"
  83.         app.VideoDisplayBase.goFullScreen(self)
  84.         self.videoFullscreen()
  85.  
  86.     def exitFullScreen(self):
  87.         print "VideoDisplay exit fullscreen"
  88.         app.VideoDisplayBase.exitFullScreen(self)
  89.  
  90.     def setVolume(self, level):
  91.         print "VideoDisplay set volume %s" % level
  92.         app.VideoDisplayBase.setVolume(self, level)
  93.         self.videoSetVolume(str(level))
  94.  
  95.     def setRate(self, rate):
  96.         print "VideoDisplay set rate %s" % rate
  97.         #app.VideoDisplayBase.setRate(self, rate)
  98.         self.videoSetRate(str(rate))
  99.  
  100.     def muteVolume(self):
  101.         print "VideoDisplay mute volume"
  102.         app.VideoDisplayBase.muteVolume(self)
  103.  
  104.     def restoreVolume(self):
  105.         print "VideoDisplay restore volume"
  106.         app.VideoDisplayBase.restoreVolume(self)
  107.  
  108.     def onSelected(self, frame):
  109.         print "VideoDisplay on selected"
  110.         app.VideoDisplayBase.onSelected(self, frame)
  111.  
  112.         # Reset the display so it can be reused
  113.         # Note from Ben: there is a race condition here if we get 2 HTTP
  114.         # requests for the VideoDisplay page in a very short timespan.  If
  115.         # VideoDisplay.onSelected() gets called for both requests, before the
  116.         # XUL code loads either page, then the second time the XUL code loads
  117.         # the page it won't be in pendingDocuments anymore.  I think it's
  118.         # exteremly unlikely that we'll ever run into this problem though.
  119.         html = template.fillStaticTemplate("video-display-vlc", {'eventCookie':self.getEventCookie(),'dtvPlatform':'xul'})
  120.  
  121.         frontend_implementation.HTMLDisplay.pendingDocuments[self.getEventCookie()] = ("text/html", html)
  122.  
  123.     frontend_implementation.HTMLDisplay.HTMLDisplay.cookieToInstanceMap[self.eventCookie] = self
  124.  
  125.         self.mutationOutput = None
  126.         self.queue = []
  127.  
  128.     def onDeselected(self, frame):
  129.         print "VideoDisplay deselected"
  130.         app.VideoDisplayBase.onDeselected(self, frame)
  131.  
  132.     def onURLLoad(self, url):
  133.         print "DTV video: %s" % url
  134.         # FIXME we probably should have some sort of a controller here
  135.         if ("action:videoPlayPause" == url):
  136.             self.playbackController.playPause()
  137.             return False
  138.         elif ("action:videoFullscreen" == url):
  139.             self.goFullScreen()
  140.             return False
  141.         elif ("action:videoStop" == url):
  142.             self.playbackController.stop()
  143.             return False
  144.         elif ("action:videoNext" == url):
  145.             self.playbackController.skip(1)
  146.             return False
  147.         elif ("action:videoPrev" == url):
  148.             self.playbackController.skip(-1)
  149.             return False
  150.         elif ("action:videoEnablePauseButton" == url):
  151.             execChromeJS('videoEnablePauseButton();')
  152.             return False
  153.         elif ("action:videoDisablePauseButton" == url):
  154.             execChromeJS('videoDisablePauseButton();')
  155.             return False
  156.         elif ("action:videoPrev" == url):
  157.             self.playbackController.skip(-1)
  158.             return False
  159.         elif (url.startswith("action:setVolume?level=")):
  160.             self.setVolume(float(url[23:]))
  161.             return False
  162.         elif (url.startswith("action:setRate?rate=")):
  163.             self.setRate(float(url[20:]))
  164.             return False
  165.         elif (url.startswith("action:enableVideoControls")):
  166.             execChromeJS('videoEnableControls();')
  167.             return False
  168.         elif (url.startswith("action:disableVideoControls")):
  169.             execChromeJS('videoDisableControls();')
  170.             return False
  171.         elif (url.startswith("action:setVideoProgress?pos=")):
  172.             self.videoSetPos(url[28:])
  173.             return False
  174.         elif (url.startswith("action:updateVideoControls?")):
  175.             elapsed = self.elapseRe.search(url).group(1)
  176.             length = self.lengthRe.search(url).group(1)
  177.             execChromeJS('videoProgressUpdate(%s, %s);' % (elapsed, length))
  178.         return True
  179.  
  180. class VLCPluginRenderer (app.VideoRenderer):
  181.  
  182.     def __init__(self):
  183.         app.VideoRenderer.__init__(self)
  184.         print "Renderer initialized"
  185.  
  186.     def reset(self):
  187.         print "Renderer reset"
  188.  
  189.     def canPlayItem(self, item):
  190.         print "canPlayItem"
  191.         return True
  192.  
  193.     def selectItem(self, item):
  194.         print "Renderer select item"
  195.         self.itemPath = item.getFilename()
  196.  
  197.     def play(self):
  198.         print "Renderer play"
  199.  
  200.     def pause(self):
  201.         print "Renderer pause"
  202.  
  203.     def stop(self):
  204.         print "Renderer stop"
  205.  
  206.     def goToBeginningOfMovie(self):
  207.         print "Renderer go to beginning"
  208.  
  209.     def getDuration(self):
  210.         print "Renderer getDuration"
  211.         return 0
  212.  
  213.     def getCurrentTime(self):
  214.         print "Renderer getCurrentTime"
  215.         return 0
  216.  
  217.     def setCurrentTime(self, time):
  218.         print "Renderer Set current time"
  219.  
  220.     def getRate(self):
  221.         print "Renderer get rate"
  222.         return 0.0
  223.  
  224.     def setRate(self, rate):
  225.         print "Renderer set rate"
  226.         
  227.     def setVolume(self, level):
  228.         print "Renderer set volume %s" % level
  229.  
  230. ###############################################################################
  231. #### Playlist item base class                                              ####
  232. ###############################################################################
  233.  
  234. class PlaylistItem:
  235.     "The record that makes up VideoDisplay playlists."
  236.  
  237.     def getTitle(self):
  238.         """Return the title of this item as a string, for visual presentation
  239.         to the user."""
  240.         raise NotImplementedError
  241.  
  242.     def getPath(self):
  243.         """Return the full path in the local filesystem to the video file
  244.         to play."""
  245.         raise NotImplementedError
  246.  
  247.     def getLength(self):
  248.         """Return the length of this item in seconds as a real number. This
  249.         is used only cosmetically, for telling the user the total length
  250.         of the current playlist and so on."""
  251.         raise NotImplementedError
  252.  
  253.     def onViewed(self):
  254.         """Called by the frontend when a clip is at least partially watched
  255.         by the user. To handle this event, for example by marking the
  256.         item viewed in the database, override this method in a subclass."""
  257.         raise NotImplementedError
  258.  
  259. ###############################################################################
  260. ###############################################################################
  261.